/*
 * CurrencyConvertorFrame.java
 *
 * Created on 28-Sep-2011, 19:15:23
 * @author Alan.Ryan
 */
package src;

import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class CurrencyConvertorFrame extends javax.swing.JFrame {
    
    //store conversion rates in an array
    //in real life app these might come from a file
    double [] conversionRates = { 1.02, 0.73,30.99, 0.63 };
  
    /** Creates new form CurrencyConvertorFrame */
    public CurrencyConvertorFrame() {
        
       initComponents();
         
       //store exchange rate textfields in an array 
       JTextField [] exchangeRateArray = {canadianExchangeRate,frenchEuroExchangeRate, bahtExchangeRate, poundsExchangeRate };
       
       //put current exchange rates into the textfields
       for (int x = 0; x < exchangeRateArray.length; x++) {
            exchangeRateArray[x].setText("" + conversionRates[x]);
        }//end for
     
    }//end CurrencyConvertorFrame()

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dollarEntryPanel = new javax.swing.JPanel();
        usDollarLabel = new javax.swing.JLabel();
        usDollarTextfield = new javax.swing.JTextField();
        convertButton = new javax.swing.JButton();
        convertedAmtPanel = new javax.swing.JPanel();
        currencyColumnLabel = new javax.swing.JLabel();
        exchangeRatecColumnLabel = new javax.swing.JLabel();
        convertedAmountColumnLabel = new javax.swing.JLabel();
        canadianDollarsLabel = new javax.swing.JLabel();
        canadianExchangeRate = new javax.swing.JTextField();
        canadianConvertedAmount = new javax.swing.JTextField();
        frenchEuroLabel = new javax.swing.JLabel();
        frenchEuroExchangeRate = new javax.swing.JTextField();
        frenchEuroConvertedAmount = new javax.swing.JTextField();
        bahtLabel = new javax.swing.JLabel();
        bahtExchangeRate = new javax.swing.JTextField();
        bahtConvertedAmount = new javax.swing.JTextField();
        poundsLabel = new javax.swing.JLabel();
        poundsExchangeRate = new javax.swing.JTextField();
        poundsConvertedAmount = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Localised Currency Convertor");

        dollarEntryPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Enter Dollar Amount"));
        dollarEntryPanel.setLayout(new java.awt.GridLayout(1, 3, 5, 5));

        usDollarLabel.setText("US Dollars");
        dollarEntryPanel.add(usDollarLabel);
        dollarEntryPanel.add(usDollarTextfield);

        convertButton.setText("Convert");
        convertButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                convertButtonActionPerformed(evt);
            }
        });
        dollarEntryPanel.add(convertButton);

        getContentPane().add(dollarEntryPanel, java.awt.BorderLayout.PAGE_START);

        convertedAmtPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Exchange Rates"));
        convertedAmtPanel.setLayout(new java.awt.GridLayout(5, 3, 5, 5));

        currencyColumnLabel.setText("Currency");
        convertedAmtPanel.add(currencyColumnLabel);

        exchangeRatecColumnLabel.setText("Exchange Rates");
        convertedAmtPanel.add(exchangeRatecColumnLabel);

        convertedAmountColumnLabel.setText("Converted Amount");
        convertedAmtPanel.add(convertedAmountColumnLabel);

        canadianDollarsLabel.setText("Canadian Dollars");
        convertedAmtPanel.add(canadianDollarsLabel);

        canadianExchangeRate.setEditable(false);
        convertedAmtPanel.add(canadianExchangeRate);

        canadianConvertedAmount.setEditable(false);
        convertedAmtPanel.add(canadianConvertedAmount);

        frenchEuroLabel.setText("Euro (France)");
        convertedAmtPanel.add(frenchEuroLabel);

        frenchEuroExchangeRate.setEditable(false);
        convertedAmtPanel.add(frenchEuroExchangeRate);

        frenchEuroConvertedAmount.setEditable(false);
        convertedAmtPanel.add(frenchEuroConvertedAmount);

        bahtLabel.setText("Thai Baht");
        convertedAmtPanel.add(bahtLabel);

        bahtExchangeRate.setEditable(false);
        convertedAmtPanel.add(bahtExchangeRate);

        bahtConvertedAmount.setEditable(false);
        convertedAmtPanel.add(bahtConvertedAmount);

        poundsLabel.setText("British Pounds");
        convertedAmtPanel.add(poundsLabel);

        poundsExchangeRate.setEditable(false);
        convertedAmtPanel.add(poundsExchangeRate);

        poundsConvertedAmount.setEditable(false);
        convertedAmtPanel.add(poundsConvertedAmount);

        getContentPane().add(convertedAmtPanel, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void convertButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_convertButtonActionPerformed
        
        //store converted amount textfields in an array
        JTextField [] convertedAmountArray = {canadianConvertedAmount,frenchEuroConvertedAmount, bahtConvertedAmount, poundsConvertedAmount };
        
        //get users input from textfield
        String amtEntered = usDollarTextfield.getText();
        
        double dollarAmt = 0.0;
        
        
        
        //convert users input from String to double
        //if parse unsuccessful - show error msg and
        //clear all converted amount textfields and users input textfield of input
        
        try {
            dollarAmt = Double.parseDouble(amtEntered);
        }//end try
                
        catch (NumberFormatException nfe) {
            JOptionPane.showMessageDialog(this, "Numeric Input Only" );
             
            for (int x = 0; x < convertedAmountArray.length; x++) {
                 convertedAmountArray[x].setText("");
                 usDollarTextfield.setText("");
             }//end for
            
            return;
            
        }//end catch
        
       
        
        //store all 4 locale objects in an array 
        Locale [] locales = {new Locale("ca", "CA"), new Locale("fr", "FR"),new Locale("th", "TH"),new Locale("gb", "GB")};  
        
        //define an array of type NumberFormat to hold a numberformat object for each locale
        NumberFormat [] currencyForm = new NumberFormat[4];
        
        
        for (int x = 0; x < convertedAmountArray.length; x++) {
            
            //store a number format object for each locale in array
            currencyForm[x] = NumberFormat.getCurrencyInstance(locales[x]);
            
        }//end for
        
        
        for (int x = 0; x < convertedAmountArray.length; x++) {
            
            //do conversion
            double amt = dollarAmt * conversionRates[x];
            
            //format converted amt for each currency based on its locale
            convertedAmountArray[x].setText(currencyForm[x].format(amt));
            
        }//end for
        
        
          
          
    }//GEN-LAST:event_convertButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CurrencyConvertorFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField bahtConvertedAmount;
    private javax.swing.JTextField bahtExchangeRate;
    private javax.swing.JLabel bahtLabel;
    private javax.swing.JTextField canadianConvertedAmount;
    private javax.swing.JLabel canadianDollarsLabel;
    private javax.swing.JTextField canadianExchangeRate;
    private javax.swing.JButton convertButton;
    private javax.swing.JLabel convertedAmountColumnLabel;
    private javax.swing.JPanel convertedAmtPanel;
    private javax.swing.JLabel currencyColumnLabel;
    private javax.swing.JPanel dollarEntryPanel;
    private javax.swing.JLabel exchangeRatecColumnLabel;
    private javax.swing.JTextField frenchEuroConvertedAmount;
    private javax.swing.JTextField frenchEuroExchangeRate;
    private javax.swing.JLabel frenchEuroLabel;
    private javax.swing.JTextField poundsConvertedAmount;
    private javax.swing.JTextField poundsExchangeRate;
    private javax.swing.JLabel poundsLabel;
    private javax.swing.JLabel usDollarLabel;
    private javax.swing.JTextField usDollarTextfield;
    // End of variables declaration//GEN-END:variables
        
    

}
